In this section there are two examples of Java Script, that demonstrate showing the local time of the visitor:
Output of time on HTML page:
Code in HTML header (before </head>):
<script language="JavaScript">
<!--
var timerID = null;
var timerRunning = false;
var id,pause=0,position=0;
function stopclock (){
if(timerRunning)
clearTimeout(timerID);
timerRunning = false;
}
function showtime () {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds()
var timeValue = "" + hours
timeValue += ((minutes < 10) ? ":0" : ":") + minutes
timeValue += ((seconds < 10) ? ":0" : ":") + seconds
document.clock.face.value = timeValue;
timerID = setTimeout("showtime()",1000);
timerRunning = true;
}
function startclock () {
stopclock();
showtime();
} //-->
</script>
Code in page body:
<body onLoad="startclock()">
<form name="clock" onSubmit="0">
<input type="text" name="face" size=13 value="">
</form>
Time output in the StatusLine of browser:
You can see an example in the StatusLine of your browser.
Code in HTML header (before </head>):
<script Language="JavaScript">
<!--
var timerID = null;
var timerRunning = false;
function stopclock(){
if(timerRunning)
clearTimeout(timerID);
timerRunning = false;
}
function startclock() {
stopclock();
showtime();
}
function showtime() {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds()
var timeValue = "Current time: " + ((hours >12) ? hours -12 :hours)
timeValue += ((minutes < 10) ? ":0" : ":") + minutes
timeValue += ((seconds < 10) ? ":0" : ":") + seconds
timeValue += (hours >= 12) ? " P.M." : " A.M."
window.status = timeValue;
timerID = setTimeout("showtime()",1000);
timerRunning = true;
}
//-->
</script>
In <body> tag it is necessary to add the starting clock function:
<body onLoad="startclock()">